﻿/*kede公共方法*/
var kede = {
    description: { version: "kede 1.0 版权所有，翻版必究", author: "merlin", date: "2015/05/13" },
    /*获取Url中的参数值*/
    QueryString: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURI(r[2]);
        
        return null;
    },
    /*时间格式化*/
    dateformat: function (date, format) {
        return date.format(format);
    },
    /*Json格式化字符串转时间格式化 /Date(xxxxxxxxxxxxx)/	   */
    jsonToDate: function (str, format) {
        var data = eval('new ' + str.replace(/\//g, ""));
        return this.dateformat(data, format);
    },
    /* 转换为千分制 */
    toThousands: function (num, length) {
        if (num == null || num == undefined)
            return "";
        return num.toFixed(length).replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
    },
    toPrecisions: function (num, length) {
        if (num == null || num == undefined)
            return "";
        return num.toFixed(length);
    },
/* 转换为百分比 */
    toPercentages: function (num, length) {
       
        if (num == null || num == undefined)
            return "";
        return (num*100).toFixed(length)+'%';
    },
    /*
    *获取本年周的开始和结束时间
    **/
    getWeek: function (week) {
        week -= 1;/*timespan*/
        var timespan = 86400000;/*60 * 60 * 1000 * 24*/
        var theDay = new Date();
        theDay.setMonth(0);
        theDay.setDate(1);
        theDay = theDay.getTime() - theDay.getDay() * timespan;
        var startTime = new Date(theDay + week * 7 * timespan);
        var endTime = new Date(theDay + (week + 1) * 7 * timespan);
        var theObj = new Object();
        theObj.startWeek = this.dateformat(startTime, "yyyy-MM-dd");
        theObj.EndTime = this.dateformat(endTime, "yyyy-MM-dd");
        return theObj;
    },
    /*获取n位随机数 n<18*/
    getRandomNumber: function (n) {
        return Math.random().toString().slice(2, 2 + n);
    },
    /*获取当前时间*/
    now: function () {
        return this.dateformat(new Date(), "yyyy-MM-dd hh:mm:ss");
    },
    //打印
    print: function (message) {
        if (typeof (message) === 'object') {
            console.log(JSON.stringify(message) + " \r\n %cTime:" + new Date(), "color:red");
        } else
            console.log(message + " \r\n %c Time:" + new Date(), "color:red");
    },
    isNull: function (str) {
        if (!str) {
            return true;
        } else {
            return str.length <= 0;
        }
    },
    strbu: function (str, number, end) {
        return str.toString().substring(number, end);
    },
    getDateZone: function (val) {
        /*转换为带时区的时间格式*/
        return this.dateformat(new Date(val), "yyyy-MM-ddThh:mm:ss+08:00");
    },
    timeFormat: {
        language: 'zh-CN',
        format: "yyyy-mm-dd hh:ii",
        autoclose: true,
        todayBtn: true,
        pickerPosition: "bottom-left"
    }
};
//kede.token = "";
Object.defineProperty(kede, "token", {
    get: function () {
        let _data = Cookies.get('kedevn');
        return _data;
    }
});


Date.prototype.format = function (format) {
    format = format || "yyyy-MM-dd hh:mm:ss";
    var o = {
        "y+": this.getFullYear(),
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    };
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
            RegExp.$1.length == 1 ? o[k] :
                ("00" + o[k]).substr(("" + o[k]).length));
    return format;
};

kede.post = function (_url, _params, _fun, _datatype, _errorfun) {
    _datatype = _datatype || "json";
    if (_datatype === "json") {
        _contentType = "application/json";
        _paramsData = _params;
    }
    
    axios.post(_url, _params)
        .then(_fun).catch(_errorfun);

    

};
kede.get = function (_url, _paramsData, _fun, _datatype,_errorfun) {
    _datatype = _datatype || "json";
    axios({
        method: 'get',
        headers: { token: kede.token },
        url: _url,
        params: _paramsData,
        // `responseType` 表示服务器响应的数据类型，可以是 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'
        responseType: _datatype, // 默认的
    }).then(_fun).catch(_errorfun);

};


kede.slimScroll = function (eid, _height) {
    $("#" + eid).slimScroll({
        height: _height + 'px'
    });
};

String.prototype.padLeft = Number.prototype.padLeft = function (length, char) {
    if (!length) return this;

    length = parseInt(length);
    char = (char == null || (typeof char === 'undefined')) ? ' ' : String(char);
    var _s = String(this);
    var _len = _s.length;
    var padLen = length - _len;
    if (padLen <= 0) return this;
    for (var i = 0; i < padLen; i++) {
        _s = char + _s;
    }
    return _s;
};
// padRight 函数实现
String.prototype.padRight = Number.prototype.padRight = function (length, char) {
    if (!length) return this;

    length = parseInt(length);
    char = (char == null || (typeof char === 'undefined')) ? ' ' : String(char);
    var _s = String(this);
    var _len = _s.length;
    var padLen = length - _len;
    if (padLen <= 0) return this;
    for (var i = 0; i < padLen; i++) {
        _s += char;
    }
    return _s;
};

kede.formatRate = function (val = 0, _length = 3) {
    if (val === null || val === undefined) {
        let _v = "0.".padEnd(_length+2, '0');
        return _v + '%';
    }
    return  (val * 100).toFixed(_length) + '%';
};
//(val * 100).toFixed(3) + '%';